######################################################
# pppoe_iperf_script
# description: multi-interface iperf3 speedtest script
# usage:
#       server: [count >= ppp interface count]
#         docker container run --rm --init --net=host --name iperf -e COUNT=500 cseelye/iperf-multi
#
#   client:
#      bash multi_iperf.sh
#
# author:      Fenei
# created:     20220804
# modified+1:
# version:     1.0
#
######################################################
[ $(yum list installed | grep iperf3 | wc -l ) -gt 0 ] || rpm -ivh http://file.xipunet.com/linux/iperf3-3.1.3-1.fc24.x86_64.rpm
rm -rf /tmp/ppp_speed_out
server_ipv4_array=("113.143.100.145" "1.86.232.37")
server_ipv6_array=("240e:658:2400:202::101" "240e:658:2400:202::201")
#array_num_4=`echo $RANDOM%${#server_ipv4_array[@]} |bc`

#array_num_6=`echo $RANDOM%${#server_ipv6_array[@]}|bc `


iperf_ppp_ipv4(){
#server_ip=()
echo "Fenei multi-interface iperf3 script"
echo "=============PPPOE-IPV4================="
printf "%-12s |%-15s |%-16s |%s\n" interface pppuser ip outbond
	for i in `ls /etc/sysconfig/network-scripts/ifcfg-ppp*`
		do
			# get ppp interface name
			inet_port=`echo $i | awk -F- '{print $NF}'`
			# get ppp interface ip
			inet_ipv4=`ip r | grep -v grep | grep -w $inet_port | awk 'END {print $NF}'`
			# get ppp user
			inet_user=$(cat /etc/sysconfig/network-scripts/ifcfg-$inet_port |grep USER=|awk -F USER= '{print $2}')
			{
					speed_out=""
					# to prevent dead loops, use until command here
					until [ "$speed_out" != "" ]
					do
						server_ipv4=${server_ipv4_array[$(echo $RANDOM%${#server_ipv4_array[@]} |bc)]}
						# Check the PPP port can ping the server
						ping -c 1 -I $inet_port $server_ipv4 &> /dev/null
						if [ $? -eq 0 ];then
								# get the sender value in iperf result
								speed_out=$(iperf3 -p $((5200+(RANDOM%30))) -c $server_ipv4 -B $inet_ipv4 -P 5 -t 60 | grep SUM |grep sender | awk '{print $6}' &)
						else
								# if PPP port can't ping the server,set sender value 0
								speed_out=0
								break
						fi
					done
					echo $speed_out >> /tmp/ppp_speed_out
					printf "%-12s |%-15s |%-16s |%s %s\n" $inet_port $inet_user $inet_ipv4 $speed_out Mbits/sec
			} &
		done
	wait
	# sum sender value
	echo Total Outbond $(awk '{s+=$1} END {print s}' /tmp/ppp_speed_out) Mbits/sec
}

iperf_ppp_ipv6(){
#server_ip=()
echo "Fenei multi-interface iperf3 script"
echo "=============PPPOE-IPV6================="
printf "%-12s |%-15s |%-42s |%s\n" interface pppuser ip outbond
	for i in `ls /etc/sysconfig/network-scripts/ifcfg-ppp*`
		do
			# get ppp interface name
			inet_port=`echo $i | awk -F- '{print $NF}'`
			# get ppp interface ip
			inet_ipv6=`ip addr show dev $inet_port | grep global |sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d'`
			# get ppp user
			inet_user=$(cat /etc/sysconfig/network-scripts/ifcfg-$inet_port |grep USER=|awk -F USER= '{print $2}')
			{
					speed_out=""
					# to prevent dead loops, use until command here
					until [ "$speed_out" != "" ]
					do
						server_ipv6=${server_ipv6_array[$(echo $RANDOM%${#server_ipv6_array[@]}|bc)]}
						# Check the PPP port can ping the server
						ping -6 -c 1 -I $inet_port $server_ipv6 &> /dev/null
						if [ $? -eq 0 ];then
								# get the sender value in iperf result
								speed_out=$(iperf3 -p $((5200+(RANDOM%30))) -c $server_ipv6 -B $inet_ipv6 -P 5 -t 20 | grep SUM |grep sender | awk '{print $6}' &)
						else
								# if PPP port can't ping the server,set sender value 0
								speed_out=0
								break
						fi
					done
					echo $speed_out >> /tmp/ppp_speed_out
					printf "%-12s |%-15s |%-42s |%s %s\n" $inet_port $inet_user $inet_ipv6 $speed_out Mbits/sec
			} &
		done
	wait
	# sum sender value
	echo Total Outbond $(awk '{s+=$1} END {print s}' /tmp/ppp_speed_out) Mbits/sec
}

iperf_eth_ipv4(){
#server_ip=()
echo "Fenei multi-interface iperf3 script"
echo "=============ETH-IPV4================="
printf "%-12s |%-16s |%s\n" interface ip outbond
	for i in `ifconfig | grep ^[a-z] | awk -F: '{print $1}' |grep -v "ppp"|grep -v "lo"`
		do
			# get ppp interface name
			inet_port=$i
			# get ppp interface ip
			inet_ipv4=`ip r | grep -v grep | grep -w $inet_port | awk 'END {print $9}'`
			{
					speed_out=""
					# to prevent dead loops, use until command here
					until [ "$speed_out" != "" ]
					do
						server_ipv4=${server_ipv4_array[$(echo $RANDOM%${#server_ipv4_array[@]} |bc)]}
						# Check the PPP port can ping the server
						ping -c 1 -I $inet_port $server_ipv4 &> /dev/null
						if [ $? -eq 0 ];then
								# get the sender value in iperf result
								speed_out=$(iperf3 -p $((5200+(RANDOM%30))) -c $server_ipv4 -B $inet_ipv4 -P 5 -t 60 | grep SUM |grep sender | awk '{print $6}' &)
						else
								# if PPP port can't ping the server,set sender value 0
								speed_out=0
								break
						fi
					done
					echo $speed_out >> /tmp/ppp_speed_out
					printf "%-12s |%-16s |%s %s\n" $inet_port $inet_ipv4 $speed_out Mbits/sec
			} &
		done
	wait
	# sum sender value
	echo Total Outbond $(awk '{s+=$1} END {print s}' /tmp/ppp_speed_out) Mbits/sec
}


iperf_eth_ipv6(){
#server_ip=()
echo "Fenei multi-interface iperf3 script"
echo "=============ETH-IPV6================="
printf "%-12s |%-42s |%s\n" interface ip outbond
	for i in `ifconfig | grep ^[a-z] | awk -F: '{print $1}' |grep -v "ppp"|grep -v "lo"`
		do
			# get ppp interface name
			inet_port=$i
			# get ppp interface ip
			inet_ipv6=`ip addr show dev $inet_port | grep global |sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d' | head -1`
			{
					speed_out=""
					# to prevent dead loops, use until command here
					until [ "$speed_out" != "" ]
					do
						server_ipv6=${server_ipv6_array[$(echo $RANDOM%${#server_ipv6_array[@]}|bc)]}
						# Check the PPP port can ping the server
						ping -6 -c 1 -I $inet_port $server_ipv6 &> /dev/null
						if [ $? -eq 0 ];then
								# get the sender value in iperf result
								speed_out=$(iperf3 -p $((5200+(RANDOM%30))) -c $server_ipv6 -B $inet_ipv6 -P 5 -t 20 | grep SUM |grep sender | awk '{print $6}' &)
						else
								# if PPP port can't ping the server,set sender value 0
								speed_out=0
								break
						fi
					done
					echo $speed_out >> /tmp/ppp_speed_out
					printf "%-12s |%-42s |%s %s\n" $inet_port $inet_ipv6 $speed_out Mbits/sec
			} &
		done
	wait
	# sum sender value
	echo Total Outbond $(awk '{s+=$1} END {print s}' /tmp/ppp_speed_out) Mbits/sec
}

while true
do
    echo -e "\033[33m -------------------------------- \033[0m"
    echo -e "\033[33m Fenei network speedtest script \033[0m"
    echo -e "\033[33m -------------------------------- \033[0m"	
	echo -e "\033[33m usage: bash multi_iperf.sh  \033[0m"
    echo -e "\033[33m -------------------------------- \033[0m"
	echo -e "PPPoe ipv4 test  :\033[32m 1 \033[0m"       
	echo -e "PPPoe ipv6 test  :\033[32m 2 \033[0m" 
	echo -e "Eth   ipv4 test  :\033[32m 3 \033[0m"       
	echo -e "Eth   ipv6 test  :\033[32m 4 \033[0m" 

    read -t 10 -p "Please input your chose: " num

    case $num in
		1)	iperf_ppp_ipv4
			;;
		2)  iperf_ppp_ipv6
            ;;
		3)	iperf_eth_ipv4
			;;
		4)  iperf_eth_ipv6
            ;;
        *)
			break
            ;;
        esac
	break
done
